package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Patent;
import com.ruoyi.system.service.IPatentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 专利信息列表Controller
 * 
 * @author chen
 * @date 2023-07-30
 */
@RestController
@RequestMapping("/system/patent")
public class PatentController extends BaseController
{
    @Autowired
    private IPatentService patentService;

    /**
     * 查询专利信息列表列表
     */
    @PreAuthorize("@ss.hasPermi('system:patent:list')")
    @GetMapping("/list")
    public TableDataInfo list(Patent patent)
    {
        startPage();
        List<Patent> list = patentService.selectPatentList(patent);
        return getDataTable(list);
    }

    /**
     * 导出专利信息列表列表
     */
    @PreAuthorize("@ss.hasPermi('system:patent:export')")
    @Log(title = "专利信息列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Patent patent)
    {
        List<Patent> list = patentService.selectPatentList(patent);
        ExcelUtil<Patent> util = new ExcelUtil<Patent>(Patent.class);
        util.exportExcel(response, list, "专利信息列表数据");
    }

    /**
     * 获取专利信息列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:patent:query')")
    @GetMapping(value = "/{patentId}")
    public AjaxResult getInfo(@PathVariable("patentId") Long patentId)
    {
        return success(patentService.selectPatentByPatentId(patentId));
    }

    /**
     * 新增专利信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:patent:add')")
    @Log(title = "专利信息列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Patent patent)
    {
        return toAjax(patentService.insertPatent(patent));
    }

    /**
     * 修改专利信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:patent:edit')")
    @Log(title = "专利信息列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Patent patent)
    {
        return toAjax(patentService.updatePatent(patent));
    }

    /**
     * 删除专利信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:patent:remove')")
    @Log(title = "专利信息列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{patentIds}")
    public AjaxResult remove(@PathVariable Long[] patentIds)
    {
        return toAjax(patentService.deletePatentByPatentIds(patentIds));
    }

    @Log(title = "专利信息列表", businessType = BusinessType.IMPORT) // todo
    @PreAuthorize("@ss.hasPermi('system:patent:import')") // todo
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Patent> util = new ExcelUtil<>(Patent.class); // todo
        List<Patent> patentList = util.importExcel(file.getInputStream()); // todo
        String operName = getUsername();
        String message = patentService.importUser(patentList, updateSupport, operName); // todo
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Patent> util = new ExcelUtil<>(Patent.class); // todo
        util.importTemplateExcel(response, "专利信息");
    }

}
